﻿/*****************************************************************************
 
 Use of Ext is under the Ext JS license, for more information, 
 please visit http://extjs.com/products/license.php page. 
  
 You should confirm that you have read, understood and accept ethical criteria listed. 
 If not, see <http://www.mvcnext.com/license/>.
  
 Version: 1.0.0.0
 Website: http://www.mvcnext.com/
 
 Copyright (c) Yoganand R. All rights reserved.
 For continued development and updates, visit http://yogsbreath.net
 
 ******************************************************************************/
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Collections;
using System.Web.UI;
using System.Text;

namespace MVCnExt.Web.UI.Ext
{
    /// <summary>
    /// A specialized grid implementation intended to mimic the traditional property grid as typically seen in development IDEs. Each row in the grid represents a property of some object, and the data is stored as a set of name/value pairs
    /// </summary>
    [ToolboxData("<{0}:PropertyGrid runat=\"server\"></{0}:PropertyGrid>")]
    [Description("A specialized grid implementation intended to mimic the traditional property grid as typically seen in development IDEs. Each row in the grid represents a property of some object, and the data is stored as a set of name/value pairs")]
    public class ExtPropertyGrid : ExtGridPanel
    {
        private List<ConfigItem> _source = new List<ConfigItem>();

        public override List<ScriptReference> GetScriptReferences()
        {
            return base.GetScriptReferences();
        }

        public override IList<ScriptDescriptor> GetScriptDescriptors()
        {
            IList<ScriptDescriptor> list = base.GetScriptDescriptors();

            ScriptControlDescriptorBase desc = (ScriptControlDescriptorBase)list[0];

            desc.Type = "Ext.grid.PropertyGrid";

            String[] properties = new String[]{
                "editable"      
            };

            Type component = typeof(ExtPropertyGrid);

            foreach (String property in properties)
            {
                PropertyInfo i = component.GetProperty(property);

                if (i == null)
                    throw new InvalidOperationException(String.Format("Property not found, 'ExtPropertyGrid.{0}'", property));

                Object value = i.GetValue(this, null);

                if (value == null)
                {
                    continue;
                }

                if (!(value is String))
                    desc.AddPropertyBase(property, value);
                else
                    desc.AddPropertyBase(property, String.Format("'{0}'", value));
            }

            //add defaults
            if (source.Count != 0)
            {
                StringBuilder config = new StringBuilder("{");

                for (int i = 0; i < source.Count; i++)
                {
                    if (i != 0)
                    {
                        config.Append(",");
#if DEBUG
                        config.AppendLine();
#endif
                    }
                    var value = source[i].value;

                    if (!(value is String))
                        config.AppendFormat("'{0}' : {1}", source[i].name, value);
                    else
                        config.AppendFormat("'{0}' : '{1}'", source[i].name, value);
                    
                }

                config.Append("}");

#if DEBUG
                config.AppendLine();
#endif

                desc.AddPropertyBase("source", config);
            }

            return list;
        }

        #region Public Properties

        [NotifyParentProperty(true)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public List<ConfigItem> source
        {
            get
            {
                return _source;
            }
        }

        /// <summary>
        /// If false then all cells will be read only
        /// </summary>
        /// <value><c>true</c> if editable; otherwise, <c>false</c>.</value>
        [Description("If false then all cells will be read only")]
        [DefaultValue(true)]
        public Boolean? editable { get; set; }

        #endregion
    }
}
